package com.crm.jdy.common.utils;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *  上传图片工具
 * @author able
 *
 */
@Component
public class UpLoadUtil {

	//图片存储路径
	private static String FILE_PHYSICAL_PATH;
	
	@Value(value="${project.file.physics-path}")
	public void setFILE_PHYSICAL_PATH(String FILE_PHYSICAL_PATH) {
		this.FILE_PHYSICAL_PATH = FILE_PHYSICAL_PATH;
	}

	/**
	 * 文件上传
	 * @param multipartFile
	 * @param relativePath 文件存放相对路径,例如: filepath/pa 或 filepah
	 * @return 文件相对路径
	 * @throws Exception
	 */
	public static String upload(MultipartFile multipartFile, String relativePath) throws IOException {

		assert(multipartFile != null);
		relativePath = pathHandle(relativePath);
		//相对路径处理
		if (StringUtils.isEmpty(relativePath)){
			relativePath = "";
		}else{
			relativePath = "/" + relativePath + "/";
		}

		//生成文件名唯一MD5名
//		String fileName = newFileName(multipartFile);
		String fileName = getUUidName(multipartFile);

		//文件相对路径
		String fileRelativePath =  null;
		fileRelativePath = relativePath  + fileName;
		File file = new File(FILE_PHYSICAL_PATH + File.separatorChar, fileRelativePath);

		System.out.println("==============");
		System.out.println(file);
		System.out.println(file.getParentFile().exists());
		//创建文件和目录
		file.getParentFile().mkdirs();
		if (!file.getParentFile().exists()) {
			file.createNewFile();
		}
		multipartFile.transferTo(file);
		return fileRelativePath;
	}


	/**
	 * 文件上传,创建日期目录
	 * @param multipartFile
	 * @param relativePath 文件存放相对路径,例如: path/path 或 path
	 * @return
	 * @throws IOException
	 */
	public static String uploadBeltDate(MultipartFile multipartFile, String relativePath) throws IOException {
		relativePath = pathHandle(relativePath);
		relativePath = relativePath + "/" + dateStr();
		return upload(multipartFile,relativePath);
	}



	/**
	 * 生成新文件唯一md5文件名
	 * @param file
	 * @return
	 */
	private static String newFileName(MultipartFile file) throws IOException {
		String fileName = file.getOriginalFilename();
		int index = fileName.lastIndexOf(".");
		assert( ! StringUtils.isEmpty(file) && index != -1);
		//生成uuid
		String md5 = DigestUtils.md5Hex(file.getBytes());
		//生成新的文件名
		String newFileName = md5 + fileName.substring(index,fileName.length());
		return newFileName;
	}

	/**
	 * 生成新文件唯一md5文件名
	 * @param file
	 * @return
	 */
	private static String getUUidName(MultipartFile file) throws IOException {
		String fileName = file.getOriginalFilename();
		int index = fileName.lastIndexOf(".");
		assert( ! StringUtils.isEmpty(file) && index != -1);
		//生成uuid
		String md5 = CommonUtils.GetGUID();
		//生成新的文件名
		String newFileName = md5 + fileName.substring(index,fileName.length());
		return newFileName;
	}

	/**
	 * 获取日期字符串
	 * @return
	 */
	private static String dateStr(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(new Date());
	}


	/**
	 * 路径处理
	 * 将路径两端的'/'去除
	 * @param path
	 * @return
	 */
	private static String pathHandle(String path){
		StringBuffer sb = new StringBuffer(path.trim());

		//去除路径首的'/'
		while (true){
			if (StringUtils.isEmpty(sb.toString())){
				return "";
			}

			if ('/' == sb.charAt(0)){
				sb.replace(0,1,"");
			}else{
				break;
			}
		}
		//去除路径尾的'/'
		while (true){
			if (StringUtils.isEmpty(sb.toString())){
				return "";
			}
			if ('/' == sb.charAt(sb.length()-1)){
				sb.replace(sb.length()-1,sb.length(),"");
			}else {
				return sb.toString();
			}
		}
	}


}
